package com.party.admin.web.controller.system;

import com.google.common.collect.Maps;
import com.party.admin.biz.system.MainBizService;
import com.party.admin.biz.system.MenuBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.AjaxResult;
import com.party.core.model.main.MainViewAnalyze;
import com.party.core.service.main.IMainViewAnalyzeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wei.li
 *
 * @date 2017/5/8 0008
 * @time 10:43
 */

@Controller
@RequestMapping(value = "/system/main/")
public class MainController {

    @Autowired
    private MainBizService mainBizService;

    @Autowired
    private MenuBizService menuBizService;

    @Autowired
    IMainViewAnalyzeService mainViewAnalyzeService;

    // 所有报名数
    public final String admin_home_allApply = "admin:home:allApply";
    // 所有订单数
    public final String admin_home_allOrder = "admin:home:allOrder";
    // 所有资金数
    public final String admin_home_allMoney = "admin:home:allMoney";
    // 所有访问数
    public final String admin_home_allVisit = "admin:home:allVisit";
    // 所有注册会员数
    public final String admin_home_allMember = "admin:home:allMember";
    // 所有实名会员数
    public final String admin_home_allRealMember = "admin:home:allRealMember";

    // 所有报名数-点击
    public final String admin_home_allApply_click = "admin:home:allApply-click";
    // 所有订单数-点击
    public final String admin_home_allOrder_click = "admin:home:allOrder-click";
    // 所有注册会员数-点击
    public final String admin_home_allMember_click = "admin:home:allMember-click";

/*
    */
/**
     * 主页视图
     * @return
     *//*

    @RequestMapping(value = "view")
    public ModelAndView view(){
        ModelAndView modelAndView = new ModelAndView("system/main/main");
        Integer unauditedMember = memberService.unauditedCount();
        String memberId = RealmUtils.getCurrentUser().getId();
        //Map<String,MerchantAnalyze> map = merchantAnalyzeBizService.adminDetail();
        Map<String,MerchantAnalyzeOutput> map = memberMerchantDetailBizService.allDetail();
        Map<String, Object> params = Maps.newHashMap();
        params.put("isCrowdfund", "0");
        params.put("payment", 0);
        Double orderTotal = orderBizService.getOrderTotal(new OrderForm(), params, false, true);
        Double refundOrderTotal = orderBizService.getOrderTotal(new OrderForm(), params, false, false);
        double allTotal = BigDecimalUtils.add(orderTotal, refundOrderTotal);
        Integer allMember = memberService.allValidCount();
        Integer allOrder = orderFormService.allValidCount();
        Integer allApply = memberActService.allCount();

        Map<String, Object> permissions = permissionMap();

        modelAndView.addObject("permissions", permissions);
        modelAndView.addObject("unauditedMember", unauditedMember);
        modelAndView.addObject("map",map);
        modelAndView.addObject("memberId",memberId);
        modelAndView.addObject("allMember", allMember);
        modelAndView.addObject("allTotal", allTotal);
        modelAndView.addObject("allOrder", allOrder);
        modelAndView.addObject("allApply", allApply);
        return modelAndView;
    }
*/


    /**
     * 主页视图
     * @return
     */
    @RequestMapping(value = "view")
    public ModelAndView view(){
        ModelAndView modelAndView = new ModelAndView("system/main/main");
        String memberId = RealmUtils.getCurrentUser().getId();

        //获取全部统计数（机构+平台）
        MainViewAnalyze mavl_total_q = mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"total","SYSTEM"));

        //获取全部统计数（机构）
        Map<String,MainViewAnalyze> map = new HashMap<>();
        map.put("today", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"today","SYSTEM")));
        map.put("yesterday", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"yesterday","SYSTEM")));
        map.put("beforeYDay", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"beforeYDay","SYSTEM")));
        map.put("week", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"week","SYSTEM")));
        map.put("month", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"month","SYSTEM")));
        map.put("history", mainViewAnalyzeService.getMainView(new MainViewAnalyze(0,"history","SYSTEM")));

        Map<String, Object> permissions = permissionMap();
        modelAndView.addObject("permissions", permissions);
        modelAndView.addObject("memberId",memberId);
        modelAndView.addObject("allMember", mavl_total_q.getMemberNum());
        modelAndView.addObject("allTotal",mavl_total_q.getMoney());
        modelAndView.addObject("allOrder",mavl_total_q.getOrderNum());
        modelAndView.addObject("allApply", mavl_total_q.getApplyNum());
        modelAndView.addObject("map",map);
        return modelAndView;
    }

    /**
     * 统计数据
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "data")
    public AjaxResult data(){
        AjaxResult ajaxResult = new AjaxResult();
        HashMap<String, List> map = Maps.newHashMap();
        List<String> dayList = mainBizService.listDay();
        List<Integer> memberList = mainBizService.memberList();
        List<Integer> applyList = mainBizService.applyList();
        List<Integer> orderList = mainBizService.orderList();
        map.put("dayList", dayList);
        map.put("memberList", memberList);
        map.put("applyList", applyList);
        map.put("orderList", orderList);
        ajaxResult.setData(map);
        return ajaxResult;
    }

    /**
     * 获取该用户的第一个菜单页面链接
     * @return
     */
    @RequestMapping(value = "firstUrl")
    @ResponseBody
    public AjaxResult firstUrl() {
        return AjaxResult.success((Object)menuBizService.firstUrl());
    }

    private Map<String, Object> permissionMap() {
        Map<String, Object> permissions = Maps.newHashMap();
        int hasPermission = 0;
        hasPermission = permissionPut(permissions, admin_home_allApply, "allApply", hasPermission);
        hasPermission = permissionPut(permissions, admin_home_allOrder, "allOrder", hasPermission);
        hasPermission = permissionPut(permissions, admin_home_allMoney, "allMoney", hasPermission);
        hasPermission = permissionPut(permissions, admin_home_allVisit, "allVisit", hasPermission);
        hasPermission = permissionPut(permissions, admin_home_allMember, "allMember", hasPermission);
        hasPermission = permissionPut(permissions, admin_home_allRealMember, "allRealMember", hasPermission);
        /*permissionPut(permissions, admin_home_allApply_click, "allApply-click");
        permissionPut(permissions, admin_home_allOrder_click, "allOrder-click");
        permissionPut(permissions, admin_home_allMember_click, "allMember-click");*/
        boolean noPermission = false;
        if (hasPermission == -6) {
            noPermission = true;
        } else {
            // 计算出下面tab的选中是哪个
            permissions.put("showType", Integer.valueOf(calcShowType(permissions)));
        }
       // permissions.put("noPermission", noPermission);
        return permissions;
    }

    private int permissionPut(Map<String, Object> map, String permission, String key, int flag) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted(permission)) {
            map.put(key, true);
            flag++;
        } else {
            map.put(key, false);
            flag--;
        }
        return flag;
    }

    private int calcShowType(Map<String, Object> permissions) {
        if (Boolean.valueOf(permissions.get("allVisit").toString())) {
            return 3;
        }
        if (Boolean.valueOf(permissions.get("allMember").toString())) {
            return 4;
        }
        if (Boolean.valueOf(permissions.get("allApply").toString())) {
            return 0;
        }
        if (Boolean.valueOf(permissions.get("allMoney").toString())) {
            return 2;
        }
        if (Boolean.valueOf(permissions.get("allOrder").toString())) {
            return 1;
        }
        return 3;
    }
}
